﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using ToolRental.Client.SMvvm.Admin.AdminService;

namespace ToolRental.Client.SMvvm.Admin
{
    public class DeviceServiceAgent : IDeviceServiceAgent
    {
        #region AddNewDevice
        public async void AddNewDevice(string stockNumber, int toolTypeId, INotifyError notifier)
        {
            try
            {
                using (var asc = new AdminServiceClient())
                {
                    await asc.AddToolAsync(stockNumber, toolTypeId);
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található!", e);
            }
        }
        #endregion

        #region GetDeviceList
        public async Task<List<Device>> GetDeviceList(int toolTypeId, INotifyError notifier)
        {
            var devices = new List<Device>();
            try
            {
                AdminService.Tool[] toolsFromService;
                using (var asc = new AdminServiceClient())
                {
                    toolsFromService = await asc.GetToolsAsync(toolTypeId);
                }
                foreach (var tool in toolsFromService)
                {
                    devices.Add(new Device()
                        {
                            DeviceTypeId = tool.ToolTypeId,
                            Id = tool.Id,
                            StockNumber = tool.StockNumber
                        });
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található", e);
            }
            return devices;
        }
        #endregion

        #region GetDeviceTypeList
        public async Task<List<DeviceType>> GetDeviceTypeList(INotifyError notifier)
        {
            List<DeviceType> deviceTypeList = new List<DeviceType>();
            try
            {
                AdminService.ToolType[] toolTypeListFromService;
                using (var asc = new AdminServiceClient())
                {
                    toolTypeListFromService = await asc.GetToolTypesAsync();
                }
                foreach (var tt in toolTypeListFromService)
                {
                    deviceTypeList.Add(new DeviceType()
                    {
                        BrandName = tt.BrandName,
                        TypeName = tt.TypeName,
                        Id = tt.Id
                    });
                }
            }
            catch (EndpointNotFoundException e)
            {
                notifier.ErrorOccured("A kiszolgáló nem található", e);
            }
            return deviceTypeList;
        }
        #endregion
    }
}
